package com.coocaa.app.lib.cleaner.u;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by lu on 17-4-17.
 */

public class AbstractFactory<T> {

    public final T create() {
        try {
            Class<T> clazz = null;
            Type t = getClass().getGenericSuperclass();
            if (t instanceof ParameterizedType) {
                Type[] args = ((ParameterizedType) t).getActualTypeArguments();
                if (args[0] instanceof Class) {
                    clazz = (Class<T>) args[0];
                }
            }
            return clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private T t = null;

    public final synchronized T instance() {
        if (t == null)
            t = create();
        return t;
    }
}
